package scuts.core;
import scuts.core.types.ProgressiveFuture;
import utest.Assert;

//import scuts.core.Collect;
using scuts.core.Collect;
class CollectTest 
{

  public function new() 
  {
    
  }
  
  public function testFuture() 
  {
    var f1 = new ProgressiveFuture();
    var f2 = new ProgressiveFuture();
    var f3 = new ProgressiveFuture();
    
    
    var f = function (i) return [f2,f3];
    
    var last = f1.collectFromArrayToArray(f);
    
    f2.deliver(1);
    f1.deliver(2);
    
    Assert.isFalse(last.isDone());
    
    f3.deliver(4);
    
    Assert.isTrue(last.isDone());
    Assert.isTrue(last.isDelivered());
    Assert.same([1,4], last.value());
  }
  
  public function testArrayFuture() 
  {
    var f1 = new ProgressiveFuture();
    var f2 = new ProgressiveFuture();
    var f3 = new ProgressiveFuture();
    
    
    var f = function (i) return [f3];
    
    var last = [f1, f2].collectFromArrayToArray(f);
    
    var l1 = last[0];
    var l2 = last[1];
    
    f2.deliver(1);
    
    Assert.isFalse(l1.isDone());
    Assert.isFalse(l2.isDone());
    
    f3.deliver(4);
    
    Assert.isTrue(l2.isDone());
    Assert.isTrue(l2.isDelivered());
    Assert.isFalse(l1.isDone());
    Assert.isFalse(l1.isCanceled());
    f1.deliver(2);
    
    
    Assert.isTrue(l1.isDone());
    Assert.isTrue(l1.isDelivered());
    
  }
  
  public function testFutureProgress() 
  {
    var f1 = new ProgressiveFuture();
    var f2 = new ProgressiveFuture();
    var f3 = new ProgressiveFuture();
    
    
    var f = function (i) return [f2,f3];
    
    var last = f1.collectFromArrayToArray(f, 0.3);
    
    var progress = 0.0;
    last.onProgress(function (p) progress = p);
    
    f1.setProgress(0.2);
    
    Assert.equals(0.3*0.2, progress);
    
    f1.deliver(1);
    
    Assert.equals(0.3, progress);
    
    f2.setProgress(0.5);
    
    Assert.equals(0.3 + 0.7 / 2 * 0.5, progress);
    
    f3.setProgress(0.5);
    
    Assert.equals(0.3 + 0.7 / 2 * 1.0, progress);
    
    f3.deliver(2);
    f2.deliver(4);
    
    Assert.equals(1.0, progress);
    
    
  }
  
}